package arina.university.controller; /**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 21.11.13
 * Time: 19:09
 * To change this template use File | Settings | File Templates.
 */

import arina.university.dao.GroupDAO;
import arina.university.dao.UniversityDAO;
import arina.university.dao.factory.DAOFactory;
import arina.university.model.Group;
import arina.university.model.Student;
import arina.university.model.University;
import arina.university.view.UniversityView;

import java.util.Collection;

public class UniversityController {
    private UniversityView view;


    public UniversityController(UniversityView view) {
        this.view = view;

    }


    DAOFactory memoryFactory = DAOFactory.getDAOFactory(DAOFactory.MEMORY);
    UniversityDAO dao = memoryFactory.getUniversityDAO();


    public University createUniversity(String name) {

        try {
            University university = dao.create(name);
            return university;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }

    }


    public void removeUniversity(String name) {

        try {
            dao.delete(name);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public University getUniversity(String name) {
        try {
            return dao.find(name);
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }

    }

    public Collection<University> getAllUniversities() {
        try {
            return dao.findAll();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    public void findUniversity(String name) {
        try {
            University university = dao.find(name);

            view.getUniversityInfo(university);
        } catch (Exception e) {
            System.out.println(e);
        }


    }

    public void updateUniversity(String name) {
        try {
            University university = dao.find(name);

            dao.delete(university.getName());
            university.setName(name);
            dao.save(university);
        } catch (Exception e) {
            System.out.println(e);
        }

    }

    public void findAllUniversities() {
        try {
            Collection<University> universities = dao.findAll();
            view.getUniversitiesInfo(universities);
        } catch (Exception e) {
            System.out.println(e);
        }

    }

    public void addGroup(String name,Group group){
        try{
            University university = dao.find(name);
            university.addGroup(group);

        }
        catch (Exception e){
            System.out.println(e);
        }
    }

    public void deleteGroup(String name, Group group){
        try{
            University university = dao.find(name);
            university.getGroups().remove(group);
        }
        catch (Exception e){
            System.out.println(e);
        }
    }




}
